package sum.book.common.core.utils;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import sum.book.common.core.constant.CodeEnum;
import sum.book.common.core.model.dto.SimpleEntity;

import java.util.Arrays;

/**
 * @Auther: ZhangGN
 * @Description:
 * @Date 2023/1/6 14:57
 **/
public class ResponseUtil {

    public static SimpleEntity getSimpleEntity(Throwable cause) {

        String message = cause.getMessage();
        System.out.println("message = " + message);
        int i = message.indexOf("[", 0) + 1;
        int i1 = message.indexOf("]", 0);
        Integer status = Integer.valueOf(message.substring(i, i1));
        int i2 = message.indexOf(": [", 0) + 3;
        int i3 = message.lastIndexOf("]");
        String msg = message.substring(i2, i3);
        return new SimpleEntity<>(msg, CodeEnum.CUSTOM_MESSAGE);
    }

    public static ResponseEntity getResponseEntity(Throwable cause) {
        String message = cause.getMessage();
        System.out.println("message = " + message);
        int i = message.indexOf("[", 0) + 1;
        int i1 = message.indexOf("]", 0);
        Integer status = Integer.valueOf(message.substring(i, i1));
        int i2 = message.indexOf(": [", 0) + 3;
        int i3 = message.lastIndexOf("]");
        String msg = message.substring(i2, i3);
        return new ResponseEntity<>(msg, HttpStatus.valueOf(status));
    }

}
